//
//  CYPostedOnlineVC.m
//  CreditAddressBook
//
//  Created by LE on 15/12/30.
//  Copyright © 2015年 Lee. All rights reserved.
//

#import "CYPostedOnlineVC.h"
#import "CYPostedOnlineCell.h"
#import "CYCreditTool.h"
#import "MBProgressHUD+MJ.h"
#import "CYNetworkCollectRecord.h"
#import "UIImageView+WebCache.h"
#import "LEDIYFooter.h"
#import "CYTool.h"
#import "LEDIYHeader.h"
#import "CYJumpsTool.h"
#import "CYPostedOnlineDetailVC.h"

@interface CYPostedOnlineVC ()<UITableViewDelegate, UITableViewDataSource>

@property(nonatomic, weak) UITableView *tableview;
@property(nonatomic, strong) NSMutableArray *networkRecords;

@property(nonatomic, assign) NSInteger page;
@end

@implementation CYPostedOnlineVC

- (NSMutableArray *)networkRecords {
    if (!_networkRecords) {
        _networkRecords = [NSMutableArray array];
    }
    return _networkRecords;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupTabel];
//    [self loadData:NO];
    [self setupRefresh];
    [CYTool setExtraCellLineHiddenWithTableView:self.tableview];
    [self setupNoti];
}

- (void)setupTabel {
    UITableView *table = [[UITableView alloc] initWithFrame:self.view.frame];
    table.delegate = self;
    table.dataSource = self;
    table.contentInset = UIEdgeInsetsMake(0, 0, 110, 0);
    [self.view addSubview:table];
    self.tableview = table;
}

- (void)loadData:(BOOL)isPull {
    if (isPull) {
        self.page = 0;
    }
    [CYCreditTool getNetworkCollectRecordsWithParam:@{@"page": @(self.page)} success:^(NSArray *defrs) {
        if (defrs && defrs.count) {
            [MBProgressHUD hideHUDForView:self.view];
            if (isPull) {
                [self.networkRecords removeAllObjects];
                [self.networkRecords addObjectsFromArray:defrs];
                [self.tableview.mj_header endRefreshing];
            } else {
                [self.networkRecords addObjectsFromArray:defrs];
                [self.tableview.mj_footer endRefreshing];
            }
            [self.tableview reloadData];
            self.page++;
        } else {
            [self.tableview.mj_footer endRefreshingWithNoMoreData];
        }
    } failure:^(NSString *message, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        [self.tableview.mj_footer endRefreshing];
        if (message) {
            [MBProgressHUD showError:message];
        } else if(error) {
            [MBProgressHUD showError:@"服务器连接错误"];
        }
    }];
}

- (void)setupRefresh {
    CYWeakObject(weakself, self);
    LEDIYFooter *footer = [LEDIYFooter footerWithRefreshingBlock:^{
        [weakself loadData:NO];
    }];
    footer.refreshingTitleHidden = YES;
    self.tableview.mj_footer = footer;
    
    LEDIYHeader *header = [LEDIYHeader headerWithRefreshingBlock:^{
        [weakself loadData:YES];
    }];
//    header.refresh
    self.tableview.mj_header = header;
    [header beginRefreshing];
}
/**
 *  监听搜索
 */
- (void)search:(NSNotification *)noti {
    NSString *searchKey = noti.userInfo[kCYCreditSearch];
    if (searchKey) {
        [MBProgressHUD showMessage:nil toView:self.view];
        [CYCreditTool getNetworkCollectRecordsBySearchWithParam:@{@"condition": searchKey, @"page": @"0"} success:^(NSArray *defrs) {
            [MBProgressHUD hideHUDForView:self.view];
            [self.networkRecords removeAllObjects];
            [self.networkRecords addObjectsFromArray:defrs];
            [self.tableview reloadData];
        } failure:^(NSString *message, NSError *error) {
            [MBProgressHUD hideHUDForView:self.view];
            if (message) {
                [MBProgressHUD showError:message];
            } else if (error) {
                [MBProgressHUD showError:@"服务器连接错误"];
            }
        }];
    }
}

#pragma mark - UITableViewDelegate UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.networkRecords.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CYNetworkCollectRecord *record = self.networkRecords[indexPath.row];
    CYPostedOnlineCell *cell = [CYPostedOnlineCell loadFromNibUsingClassName];
    cell.title.text = record.title;
    if (record.photo && !record.photo.isEmpty) {
        [cell.avatarIV sd_setImageWithURL:CYURLWithService(record.photo)];
    }
    cell.detailLabel.text = record.defaultContent;
    cell.defaulterLabel.text = CYStringFormat(@"违约人：%@", record.name);
    cell.pubTime.text = [CYCreditTool timewithLongLongValue:record.createTime.longLongValue formatter:@"yyyy-MM-dd"];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    CYNetworkCollectRecord *record = self.networkRecords[indexPath.row];
//    CYPostedOnlineDetailTVC *detail = [CYPostedOnlineDetailTVC le_loadFromStoryBoard:[CYJumpsTool mainSB]];
    CYPostedOnlineDetailVC *detail = [[CYPostedOnlineDetailVC alloc] init];
    detail.record = record;
    detail.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:detail animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 90;
}

#pragma mark -
- (void)setupNoti {
    [CYNotificationCenter addObserver:self selector:@selector(search:) name:nCYCreditSearch object:nil];
}

- (void)dealloc {
    CYLog(@"网络曝光销毁");
    [self.tableview.mj_footer endRefreshing];
}
@end
